package com.ica.android.ui.policy;

import java.util.List;
import java.util.Vector;

import com.ica.R;
import com.ica.android.ui.policy.time.TimeTableManagerActivity;
import com.ica.model.contact.ContactGroup;
import com.ica.model.facade.PolicyFacadeMockImpl;
import com.ica.model.policy.Policy;
import com.ica.model.policy.time.TimeStamp;
import com.ica.model.policy.time.utils.TimeUtils.DAYS;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class NewPolicyActivity extends PolicyActivity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {	
        super.onCreate(savedInstanceState); 
        init(R.layout.new_policy);    
    }
	
	@Override
	protected OnClickListener onClickSave() {		
		return new OnClickListener() {		
			public void onClick(View v) {
				if (etName.getText().toString().equals(""))
					Toast.makeText(NewPolicyActivity.this, "The Policy's name is required", Toast.LENGTH_SHORT).show();
				else {
					policy = new Policy(etName.getText().toString());
					List<ContactGroup> c = new Vector<ContactGroup>();
					for(int i=0;i<contacts.length;i++) {
						if (checkedContacts[i]) c.add(contacts[i]);
					}
					policy.setContacts(c);
					policy.setCommand(command);
					timeFilter = TimeTableManagerActivity.getResult();
					if (timeFilter != null) {
						for(DAYS day : DAYS.values())
							policy.addAllTimeStamps(day, new Vector<TimeStamp>(timeFilter.get(day)));
						TimeTableManagerActivity.clearResult();
					}
					PolicyFacadeMockImpl.getInstance().addElement(policy.getId(), policy);
					setResult(RESULT_OK,null);
					finish();
				}
			}
		};
	}

	@Override
	protected OnClickListener onClickTimeTable() {
		return new OnClickListener() {		
			public void onClick(View v) {
				Intent intent = new Intent(NewPolicyActivity.this, TimeTableManagerActivity.class);
				startActivity(intent);
			}
		};
	}
}
